package com.orange.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.provider.CallLog.Calls;

public class MissCallUtils {
	private static final String	TAG										= "MissCallUtils";
	private static final String	METHOD_GET_ITELEPHONY					= "getITelephony";
	private static final String	METHOD_CANCEL_MISSEDCALLS_NOTIFICATION	= "cancelMissedCallsNotification";

	public static boolean isMissedCallFromSameSender(Context context,
			List<Map<String, String>> resultList) {
		List<String> numbers = getNumbersByResult(context, resultList);
		if (null != numbers && !numbers.isEmpty()) {
			return numbers.size() == 1;
		} else {
			return true;
		}
	}

	public static List<String> getNumbersByResult(Context context,
			List<Map<String, String>> resultList) {
		List<String> ret = new ArrayList<String>();
		Map<String, String> tempMap;
		LogHelper.i(TAG,
				"getNumbersByResult. result.size: " + resultList.size());
		for (int count = 0; count < resultList.size(); count++) {
			tempMap = resultList.get(count);
			String number = tempMap.get(Calls.NUMBER);
			LogHelper.i(TAG, "getNumbersByResult. number: " + number);
			ret.add(number);
		}

		return ret;
	}

	public static void cancelMissedCallsNotification(Context context) {
		Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
		Class c = tm.getClass();
		Method getTel = null;

		try {
			getTel = c.getDeclaredMethod(METHOD_GET_ITELEPHONY, (Class[]) null);
		} catch (Exception e) {
			LogHelper.e(TAG, "cancelMissedCallsNotification. ", e);
		}

		getTel.setAccessible(true);
		try {
			Object r = getTel.invoke(tm, null); // ITelephony
			Class rc = r.getClass();
			Method getCan = null;
			getCan = rc.getDeclaredMethod(
					METHOD_CANCEL_MISSEDCALLS_NOTIFICATION, (Class[]) null);
			Object ro = getCan.invoke(r, null);

		} catch (Exception e) {
			LogHelper.e(TAG, "cancelMissedCallsNotification. ", e);
		}
	}

}
